package com.seeyon.apps.brwh.manager.impl;

import com.alibaba.fastjson.JSONObject;
import com.aspose.imaging.internal.cf.Z;
import com.seeyon.apps.brwh.kit.FormCap4Kit;
import com.seeyon.apps.brwh.kit.FormKit;
import com.seeyon.apps.brwh.kit.StrKit;
import com.seeyon.apps.brwh.manager.EarlyWarningManager;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.modules.engin.manager.CapFormDefinitionManager;
import com.seeyon.cap4.form.po.CAPFormDefinition;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.cap4.template.manager.CAPDataListSqlHelpManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.po.OrgMember;
import com.seeyon.ctp.organization.po.OrgProperties;
import com.seeyon.ctp.rest.resources.EarlyWarningResources;
import org.apache.commons.logging.Log;
import com.seeyon.ctp.common.exceptions.BusinessException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预警面板接口业务实现类
 *
 * @author ys
 * @create 2024-07-05-15:30
 */
public class EarlyWarningManagerImpl implements EarlyWarningManager {

    private static final Log log = CtpLogFactory.getLog(EarlyWarningResources.class);
    private CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
    private CapFormDefinitionManager capFormDefinitionManager = (CapFormDefinitionManager) AppContext.getBean("capFormDefinitionManager");

    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");

    private CAPDataListSqlHelpManager capDataListSqlHelpManager = (CAPDataListSqlHelpManager) AppContext.getBean("capDataListSqlHelpManager");

    /**
     * 获取预警数据
     *
     * @return
     */

    @Override
    public Map<String, Object> getEarlyWarningResources() {

        HashMap<String, Object> res = new HashMap<>();
        //
        List<Map<String, Object>> dataList = new ArrayList<>();
        //        订未入预警
        List dwrList=new ArrayList();
        //        交付延误预警
        List jfywList=new ArrayList();
        //        供应商考察-战略类没有现场考察
        List gyskcList=new ArrayList();
        //       合同未归档预警（5天以上）
        List htwgdList=new ArrayList();
        //       已下单未开票预警
        List wkpList=new ArrayList();
        //      该处罚的未处罚
        List gyscfList=new ArrayList();
        //        打样费该抵扣的未抵扣
        List wdkList=new ArrayList();
        //        采购需求超过5个工作日未询价
        List wxjList=new ArrayList();
        try {
            // 采购订单模版编号
            String cgddFormCode = AppContext.getSystemProperty("brwh.cgdddaFormCode");
            // 供应商模版编号
            String gysFormCode = AppContext.getSystemProperty("brwh.gysdaTemp");
            // 采购合同模版编号
            String cghtFormCode = AppContext.getSystemProperty("brwh.cghtddTemp");
            // 采购需求模版编号
            String cgxqFormCode = AppContext.getSystemProperty("brwh.cgxqddTemp");
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String nowDate = currentDate.format(formatter);
            /* 根据表单代码获取表单bean */
            /* 订未入预警*/
            /* 交付延误预警*/
            /* 已下单未开票预警*/
            // 采购订单
            FormBean cgddformBean = cap4FormManager.getFormByFormCode(cgddFormCode);
            // 供应商
            FormBean gysformBean = cap4FormManager.getFormByFormCode(gysFormCode);
            // 采购合同
            FormBean cghtformBean = cap4FormManager.getFormByFormCode(cghtFormCode);
            // 采购需求
            FormBean cgxqformBean = cap4FormManager.getFormByFormCode(cgxqFormCode);

            /*订未入预警*/
            //档案创建时间
            String createDate =cgddformBean.getFieldBeanByDisplay("档案创建时间").getName();
            String sfcsrk = cgddformBean.getFieldBeanByDisplay("是否完成入库").getName();
            String jhyj = cgddformBean.getFieldBeanByDisplay("是否需要交付延误预警").getName();
            //交货里程碑明细表名
            String cgddSubTableName = cgddformBean.getFieldBeanByDisplay("是否需要交付延误预警").getOwnerTableName();
            //付款状态-付款里程碑
            String jfywyj = cgddformBean.getFieldBeanByDisplay("是否需要已下单未收票预警").getName();
            //付款里程碑明细表名
            String fkTableName = cgddformBean.getFieldBeanByDisplay("预计收票时间").getOwnerTableName();
            //主表名
            String tableName = cgddformBean.getMasterTableBean().getTableName();

           /*供应商处罚*/
            String gyscfyj = gysformBean.getFieldBeanByDisplay("是否需要该罚未罚预警").getName();
            String tbTime = gysformBean.getFieldBeanByDisplay("填报日期").getName();
            String gysMainTableName = gysformBean.getFieldBeanByDisplay("是否需要该罚未罚预警").getOwnerTableName();
            //获取枚举编码
            List<CtpEnumItem> gysdjZs = FormCap4Kit.getEnumByNameList("Y");
            if (gysdjZs == null || gysdjZs.isEmpty()){
                return null;
            }
            Long enumId = gysdjZs.get(0).getId();

           /*  合同未归档预警（5天以上）*/

            /*打样费该抵扣的未抵扣*/
            String sfdk = gysformBean.getFieldBeanByDisplay("打样费是否该抵扣未抵扣").getName();
            String dyfTableName = gysformBean.getFieldBeanByDisplay("打样费是否该抵扣未抵扣").getOwnerTableName();

            /* 采购需求超过3个工作日未询价*/
            //            供应商选择流程状态
            String gysxzlczt = cgxqformBean.getFieldBeanByDisplay("供应商选择流程状态").getName();
            //            档案创建日期时间
            String dacjsj = cgxqformBean.getFieldBeanByDisplay("档案创建日期时间").getName();
            String cgxqTableName = cgxqformBean.getFieldBeanByDisplay("供应商选择流程状态").getOwnerTableName();
            ////  lczt_N  获取枚举值 Y
            List<CtpEnumItem> lczt = FormCap4Kit.getEnumByNameList("lczt_N");
            if (lczt == null || lczt.isEmpty()){
                return null;
            }
            Long lcztId = lczt.get(0).getId();


////            /* 查询sql 订未入预警*/
//            String dwrStr = "select id from " + tableName + " where DATEDIFF('" + nowDate + "', " + createDate + ") >= 30 and " + sfcsrk + " = '否' order by "+createDate+ " desc";
////            /* 查询sql  采购需求超过5个工作日未询价sql语句*/
//            String wxjStr = "select id from " + cgxqTableName + " where DATEDIFF('" + nowDate + "', " + dacjsj + ")  >= 3 and " + gysxzlczt + " = " + lcztId+" order by "+dacjsj+ " desc";
            /*订未入 ---sqlServer*/
            String dwrStr = "select id from " + tableName + " where DATEDIFF(day," + createDate + ", '" + nowDate + "') >= 30 and " + sfcsrk + " = '否' order by "+createDate+ " desc";
            /* 查询sql  采购需求超过3个工作日未询价sql语句 --sqlServer*/
            String wxjStr = "select id from " + cgxqTableName + " where DATEDIFF(day," + dacjsj + ", '" + nowDate + "')  >= 3 and " + gysxzlczt + " = " + lcztId+" order by "+dacjsj+ " desc";
            /* 查询sql  打样费该抵扣的未抵扣*/
            String wdkStr = "select id from " + dyfTableName + " where " + sfdk + " = "+enumId+" order by " + tbTime + " desc";
            /*查询sql 供应商处罚*/
            String gyscfStr = "select id from " + gysMainTableName + " where " + gyscfyj + " = " + enumId + " order by " + tbTime + " desc";
            /*SQL日志*/
            log.info("采购订单订未入中标率sql语句：" + dwrStr);
            log.info(" 采购需求超过3个工作日未询价sql语句：" + wxjStr);
            log.info("  打样费该抵扣的未抵扣sql语句：" + wdkStr);
            log.info("  供应商处罚sql语句：" + gyscfStr);
            /* 执行SQL查询 */
            dwrList = capDataListSqlHelpManager.selectDataList(dwrStr, null);
            wxjList = capDataListSqlHelpManager.selectDataList(wxjStr, null);
            wdkList = capDataListSqlHelpManager.selectDataList(wdkStr, null);
            gyscfList = capDataListSqlHelpManager.selectDataList(gyscfStr, null);


            /*配置文件跳转url*/
            String dwrUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.dwrUrlTemp"));
            String jfywUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.jfywUrlTemp"));
            String wkpUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.wkpUrlTemp"));
            String gyskcUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.gyskcUrlTemp"));
            String wxjUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.wxjUrlTemp"));
            String wdkUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.wdkUrlTemp"));
            String htwgdUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.htwgdUrlTemp"));
            String gyscfUrl = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.gycfjlsrm"));
            /* 配置文件读取内容 */
            String dwrContent = AppContext.getSystemProperty("brwh.dwrContent");
            String jfywContent = AppContext.getSystemProperty("brwh.jfywContent");
            String wkpContent = AppContext.getSystemProperty("brwh.wkpContent");
            String gyskcContent = AppContext.getSystemProperty("brwh.gyskcContent");
            String wxjContent = AppContext.getSystemProperty("brwh.wxjContent");
            String wdkContent = AppContext.getSystemProperty("brwh.wdkContent");
            String htwgdContent = AppContext.getSystemProperty("brwh.htwgdContent");
            /* 配置文件读取Title */
            String dwrTitle = AppContext.getSystemProperty("brwh.dwrTitle");
            String jfywTitle = AppContext.getSystemProperty("brwh.jfywTitle");
            String wkpTitlet = AppContext.getSystemProperty("brwh.wkpTitlet");
            String gyskcTitle = AppContext.getSystemProperty("brwh.gyskcTitle");
            String wxjTitle = AppContext.getSystemProperty("brwh.wxjTitle");
            String wdkTitle = AppContext.getSystemProperty("brwh.wdkTitle");
            String htwgdTitle = AppContext.getSystemProperty("brwh.htwgdTitle");
            if (dwrList != null && dwrList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", dwrList.get(0));
                map.put("title", dwrTitle);
                map.put("content", dwrContent);
                map.put("date", nowDate);
                map.put("url", dwrUrl);
                dataList.add(map);
            }

            if (wxjList != null && wxjList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", wxjList.get(0));
                map.put("title", wxjTitle);
                map.put("content", wxjContent);
                map.put("date", nowDate);
                map.put("url", wxjUrl);
                dataList.add(map);
            }
            if (wdkList != null && wdkList.size() > 0) {

                Map<String, Object> map = new HashMap<>();
                map.put("id", wdkList.get(0));
                map.put("title", wdkTitle);
                map.put("content", wdkContent);
                map.put("date", nowDate);
                map.put("url", wdkUrl);
                dataList.add(map);
            }
            if (gyscfList != null && gyscfList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", gyscfList.get(0));
                map.put("title", "该罚款的未罚款");
                map.put("content", "提起供应商处罚流程后结果未执行");
                map.put("date", nowDate);
                map.put("url", gyscfUrl);
                dataList.add(map);
            }
            res.put("code", 200);
            res.put("msg", "成功");
            res.put("data", dataList);
            return res;
        } catch (Exception e) {
            res.put("code", 500);
            res.put("msg", e.getMessage());
            res.put("data", "");
            return res;
        }


    }

    @Override
    public Map<String, Object> getMoreEarlyWarningUrl() {
        Map<String, Object> res = new HashMap<>();
        String url = FormCap4Kit.getBbAddres("全部");
        res.put("url", url);
        res.put("data", " ");
        res.put("code", 200);
        res.put("msg", "成功");

        return res;
    }


    /**
     * 获取项目信息数据列表
     * 此方法通过接收不同参数来查询项目信息，包括波次、项目名称、项目编码、项目经理姓名和计划结束时间
     * 它是为了灵活地根据不同的条件进行项目信息查询而设计的
     *
     * @param waves              波次，用于区分不同的项目批次
     * @param itemName           项目名称，可以用于精确查询
     * @param itemCode           项目编码，每个项目的唯一标识
     * @param projectManagerName 项目经理姓名，用于查询某个项目经理负责的项目
     * @param planEndTime        计划结束时间，用于按时间范围查询项目
     * @return 返回一个包含项目信息的列表，每个项目信息以Map形式存储，键为项目信息的属性名，值为属性值
     */
    @Override
    public List<Map<String, Object>> getProjectInfoDataList(String waves, String itemName, String itemCode, String projectManagerName, String planEndTime) throws BusinessException {
        /* 初始化需要的数据 */
        List<Map<String, Object>> resultDataList = new ArrayList<Map<String, Object>>();
        /*波次*/
        String wavesStr = "";
        /*物料名称*/
        String itemNameStr = "";
        /*物料编码*/
        String itemCodeStr = "";
        /*交付项目经理姓名*/
        String projectManagerNameStr = "";
        /*预计结束时间*/
        String planEndTimeMasterStr = "";
        /*物料大类*/
        String itemClassStr = "";
        /*计划状态*/
        String planStatusStr = "";
        /*采购需求相关字段*/
        /*采购完成百分比*/
        String purchasePercentageStr = "";
        /*供应商选择相关字段*/
        /*供应商完成百分比*/
        String supplierPercentageStr = "";
        /*合同签署相关字段*/
        /*合同签署完成百分比*/
        String contractPercentageStr = "";
        /*采购订单下发相关字段*/
        /*采购订单下发完成百分比*/
        String orderPercentageStr = "";
        /*排产上线相关字段*/
        /*排产上线完成百分比*/
        String onlinePercentageStr = "";
        /*工厂发货相关字段*/
        /*工厂发货完成百分比*/
        String deliveryPercentageStr = "";
        /*客户验收相关字段*/
        /*客户验收完成百分比*/
        String acceptancePercentageStr = "";
        /*主表名*/
        String masterTableNameStr = "";

        /*明细表字段*/
        /*责任人*/
        String responsiblePersonStr = "";
        /*计划结束时间*/
        String planEndTimeStr = "";
        /*实际完成时间*/
        String actualEndTimeStr = "";
        /*里程碑*/
        String milestoneStr = "";
        /*明细表名*/
        String detailTableNameStr = "";

        /*其他*/
        /*计划状态--进行中*/
        Long jxz = 0l;

        try {
            /*获取采购WBS档案表中对应字段*/

            String cgwbs_code = AppContext.getSystemProperty("brwh.cgwbs_code");
            FormBean formBean = cap4FormManager.getFormByFormCode(cgwbs_code);
            if (formBean != null) {
                /*主表名*/
                masterTableNameStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsbochi")).getOwnerTableName();
                // 根据显示名称获取波纹代码字段名
                wavesStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsbochi")).getName();
                // 根据显示名称获取项目名称字段名
                itemNameStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbswlmc")).getName();
                // 根据显示名称获取项目代码字段名
                itemCodeStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbwlbm")).getName();
                // 根据显示名称获取项目经理姓名字段名
                projectManagerNameStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsjfxmkl")).getName();
                // 根据显示名称获取计划结束日期字段名
                planEndTimeMasterStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsyjjssj")).getName();
                // 根据显示名称获取项目分类字段名
                itemClassStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbswldl")).getName();
                // 根据显示名称获取计划状态字段名
                planStatusStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsjhzt")).getName();
                // 根据显示名称获取采购比例字段名
                purchasePercentageStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbscgxqbfb")).getName();
                // 根据显示名称获取供应商比例字段名
                supplierPercentageStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsgysbfb")).getName();
                // 根据显示名称获取合同比例字段名
                contractPercentageStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbshtqsbfb")).getName();
                // 根据显示名称获取订单比例字段名
                orderPercentageStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbscgddxfbfb")).getName();
                // 根据显示名称获取在线比例字段名
                onlinePercentageStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbspcsxbfb")).getName();
                // 根据显示名称获取交货比例字段名
                deliveryPercentageStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsgcfhbfb")).getName();
                // 根据显示名称获取验收比例字段名
                acceptancePercentageStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbskhysbfb")).getName();
                /*明细表*/
                //明细表名
                detailTableNameStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsmxzrr")).getOwnerTableName();
                // 根据显示名称获取负责人字段名
                responsiblePersonStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsmxzrr")).getName();
                // 根据显示名称获取计划结束日期字段名
                planEndTimeStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsmxjhjssj")).getName();
                // 根据显示名称获取实际结束日期字段名
                actualEndTimeStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsmxsjwcsj")).getName();
                /*里程碑*/
                milestoneStr = formBean.getFieldBeanByDisplay(AppContext.getSystemProperty("brwh.wbsmxlcb")).getName();
                /*构建查询主表sql*/
                StringBuilder sql = new StringBuilder();
                sql.append("select id as id, ");
                /*当获取到的字段不为空时，拼接进入sql*/
                List<String> fields = new ArrayList<>();
                if (!"".equals(wavesStr)) {
                    fields.add(wavesStr);
                }
                if (!"".equals(itemNameStr)) {
                    fields.add(itemNameStr);
                }
                if (!"".equals(itemCodeStr)) {
                    fields.add(itemCodeStr);
                }
                if (!"".equals(projectManagerNameStr)) {
                    fields.add(projectManagerNameStr);
                }
                if (!"".equals(planEndTimeMasterStr)) {
                    fields.add(planEndTimeMasterStr);
                }
                if (!"".equals(itemClassStr)) {
                    fields.add(itemClassStr);
                }
                if (!"".equals(purchasePercentageStr)) {
                    fields.add(purchasePercentageStr);
                }
                if (!"".equals(supplierPercentageStr)) {
                    fields.add(supplierPercentageStr);
                }
                if (!"".equals(contractPercentageStr)) {
                    fields.add(contractPercentageStr);
                }
                if (!"".equals(orderPercentageStr)) {
                    fields.add(orderPercentageStr);
                }
                if (!"".equals(onlinePercentageStr)) {
                    fields.add(onlinePercentageStr);
                }
                if (!"".equals(deliveryPercentageStr)) {
                    fields.add(deliveryPercentageStr);
                }
                if (!"".equals(acceptancePercentageStr)) {
                    fields.add(acceptancePercentageStr);
                }

                // 将所有非空字段添加到 SQL 语句中
                for (int i = 0; i < fields.size(); i++) {
                    sql.append(fields.get(i));
                    if (i < fields.size() - 1) {
                        sql.append(", ");
                    }
                }
                /*处理开发状态 只取计划状态为进行中的 --JHZT_JXZ*/
                CtpEnumItem enumItem = FormCap4Kit.getEnumByCode("JHZT_JXZ");
                if (enumItem != null) {
                    jxz = enumItem.getId();
                }

                //根据入参处理模糊查询，在sql品拼接条件
                sql.append(" from ").append(masterTableNameStr).append(" where ")
                        .append(planStatusStr).append(" = ").append(jxz) ;

                boolean firstCondition = false;
                int appendCount = 0;

                // 检查波次字符串是否非空
                if (!"".equals(waves)) {
                    // 如果是第一个查询条件，则不添加"and"，否则添加"and"以连接查询条件
                    if (!firstCondition) {
                        sql.append(" and ");
                    }
                    // 构建波次的模糊查询条件
                    sql.append(wavesStr).append(" like '%").append(waves).append("%'");
                    // 标记第一个查询条件已经添加
                    firstCondition = false;
                    // 记录已添加的查询条件数量
                    appendCount++;
                }
                // 检查物品名称字符串是否非空
                if (!"".equals(itemName)) {
                    // 如果不是第一个查询条件，则添加"and"
                    if (!firstCondition) {
                        sql.append(" and ");
                    }
                    // 构建物品名称的模糊查询条件
                    sql.append(itemNameStr).append(" like '%").append(itemName).append("%'");
                }
                // 检查物品编码字符串是否非空
                if (!"".equals(itemCode)) {
                    // 如果不是第一个查询条件，则添加"and"
                    if (!firstCondition) {
                        sql.append(" and ");
                    }
                    // 构建物品编码的模糊查询条件
                    sql.append(itemCodeStr).append(" like '%").append(itemCode).append("%'");
                }
                // 检查项目经理名称字符串是否非空
                if (!"".equals(projectManagerName)) {
                    // 如果不是第一个查询条件，则添加"and"
                    if (!firstCondition) {
                        sql.append(" and ");
                    }
                    // 构建项目经理名称的模糊查询条件
                    // 定义变量存储项目经理的ID
                    String memberId="";
                    // 构建查询项目经理ID的SQL语句
                    StringBuilder queryMemberSql = new StringBuilder();
                    // 将查询语句动态拼接完成，查询名为projectManagerName的项目经理ID
                    queryMemberSql.append("select id as id from ORG_MEMBER where name = '").append(projectManagerName).append(" '");
                    // 执行SQL查询并获取结果列表
                    List memberIdList = capDataListSqlHelpManager.selectDataList(queryMemberSql.toString(), null);
                    // 检查查询结果是否大于0，即是否存在符合条件的项目经理
                    if(memberIdList.size()>0){
                        // 将查询结果的第一条记录转换为Map类型，以便按键获取值
                        Map<String, Object> memberIdMap = (Map<String, Object>) memberIdList.get(0);
                        // 从Map中获取项目经理ID并转换为字符串格式
                        memberId = StrKit.str(memberIdMap.get("id"));
                    }
                    // 检查项目经理ID是否不为空字符串，以确保确实找到了对应的ID
                    if (!"".equals(memberId)){
                        // 如果找到了项目经理ID，则构建并追加SQL查询条件，用于后续的查询操作
                        sql.append(projectManagerNameStr).append(" = ").append(memberId);
                    }

                }
                // 检查计划结束时间字符串是否非空
                if (!"".equals(planEndTime)) {
                    // 如果不是第一个查询条件，则添加"and"
                    if (!firstCondition) {
                        sql.append(" and ");
                    }
                    // 构建计划结束时间的查询条件
                    sql.append(planEndTimeMasterStr).append(" = '").append(planEndTime).append("'");
                }


                // 如果没有条件，则添加一个始终成立的条件
                if (appendCount == 0) {
                    sql.append(" and 1=1 ");
                }
                log.info("======项目信息查询SQL========： " + sql.toString());
                List dataList = capDataListSqlHelpManager.selectDataList(sql.toString(), null);
                /*查询结果不为空，进行数据处理*/
                if (!dataList.isEmpty()) {
                    /*遍历主数据*/
                    for (int i = 0; i < dataList.size(); i++) {
                        Map<String, Object> resultDataMap = new HashMap<String, Object>();
                        Map<String, Object> dataMap = (Map<String, Object>) dataList.get(i);
                        String masterId = StrKit.str(dataMap.get("id"));
                        /*档案表跳转url*/
                        /*获取跳转url参数*/
                        CAPFormDefinition capFormDefinition = capFormDefinitionManager.selectById(formBean.getId());
                        String appbindInfo = capFormDefinition.getAppbindInfo();
                        Map<String, Object> jsonObject = JSONObject.parseObject(appbindInfo, Map.class);
                        List<Map<String, Object>> formBindAuthList = (List<Map<String, Object>>) jsonObject.get("formBindAuthList");
                        String tempId = StrKit.str(formBindAuthList.get(0).get("id"));
                        /* 测试环境url http://124.70.102.179/*/
                        //String url = "/seeyon/cap4/businessTemplateController.do?method=formContent&type=browse&rightId=2118985888240190754.-186111276419497850&moduleId="+masterId+"&formTemplateId="+tempId+"&viewConditionId=&columnId=-5235538511930834458&moduleType=42&conditionIdDeal=&needUpdate=1&tplId=850010";
                        /* 正式环境url*/
                        String url="/seeyon/cap4/businessTemplateController.do?method=formContent&type=browse&rightId=-5722464555392153566.-8507226579802894551&moduleId="+masterId+"&formTemplateId="+tempId+"&viewConditionId=&columnId=-8036222397232627171&moduleType=42&conditionIdDeal=&needUpdate=1&tplId=850010";
                        resultDataMap.put("url", url);
                        // 从数据映射中获取"waves"对应的值，并将其作为字符串添加到结果数据映射中
                        String wavesValue = StrKit.str(dataMap.get(wavesStr));
                        resultDataMap.put("waves", wavesValue);
                        // 从数据映射中获取"itemName"对应的值，并将其作为字符串添加到结果数据映射中
                        String itemNameStrValue = StrKit.str(dataMap.get(itemNameStr));
                        resultDataMap.put("itemName", itemNameStrValue);
                        // 从数据映射中获取"itemCode"对应的值，并将其作为字符串添加到结果数据映射中
                        String itemCodeStrValue = StrKit.str(dataMap.get(itemCodeStr));
                        resultDataMap.put("itemCode", itemCodeStrValue);
                        // 从数据映射中获取"planEndTimeMaster"对应的值，并将其作为字符串添加到结果数据映射中
                        String planEndTimeMasterStrValue = StrKit.str(dataMap.get(planEndTimeMasterStr));
                        resultDataMap.put("planEndTimeMaster", planEndTimeMasterStrValue);
                        // 从数据映射中获取"projectManagerName"对应的值，进行处理后添加到结果数据映射中
                        String projectManagerNameId = StrKit.str(dataMap.get(projectManagerNameStr));
                        // 如果"projectManagerName"的值为空，则在结果数据映射中将其设置为空字符串
                        if (StrKit.isNull(projectManagerNameId)) {
                            resultDataMap.put("projectManagerName", "");
                        } else {
                            // 根据ID获取枚举项
                            V3xOrgMember member= orgManager.getMemberById(Long.valueOf(projectManagerNameId));

                            // 如果枚举项不为空，则将其显示值添加到结果数据映射中
                            if (member != null) {
                                resultDataMap.put("projectManagerName", member.getName());
                            }
                        }
                        String purchasePercentageStrValue = StrKit.str(dataMap.get(purchasePercentageStr));
                        resultDataMap.put("purchasePercentage", purchasePercentageStrValue);

                        String supplierPercentageStrValue = StrKit.str(dataMap.get(supplierPercentageStr));
                        resultDataMap.put("supplierPercentage", supplierPercentageStrValue);

                        String contractPercentageStrValue = StrKit.str(dataMap.get(contractPercentageStr));
                        resultDataMap.put("contractPercentage", contractPercentageStrValue);

                        String orderPercentageStrValue = StrKit.str(dataMap.get(orderPercentageStr));
                        resultDataMap.put("orderPercentage", orderPercentageStrValue);

                        String onlinePercentageStrValue = StrKit.str(dataMap.get(onlinePercentageStr));
                        resultDataMap.put("onlinePercentage", onlinePercentageStrValue);

                        String deliverPercentageStrValue = StrKit.str(dataMap.get(deliveryPercentageStr));
                        resultDataMap.put("deliveryPercentage", deliverPercentageStrValue);

                        String accountPercentageStrValue = StrKit.str(dataMap.get(acceptancePercentageStr));
                        resultDataMap.put("acceptancePercentage", accountPercentageStrValue);
                        /*里程碑不为空的情况下处理枚举值*/
                        /*采购需求*/
                        String cgxqEnumId = "";
                        /*供应商选择*/
                        String gysxzEnumId = "";
                        /*合同签署*/
                        String htsqEnumId = "";
                        /*采购订单下发*/
                        String cgddxfEnumId = "";
                        /*排产上线*/
                        String pcsxEnumId = "";
                        /*工厂发货*/
                        String gcfhEnumId = "";
                        /*客户验收*/
                        String khysEnumId = "";
                        if (!"".equals(milestoneStr)) {
                            /*采购需求1*/
                            cgxqEnumId=FormCap4Kit.getEnumByNameList(AppContext.getSystemProperty("brwh.wbs_enum_cgxq")).get(0).getId().toString();
                            /*供应商选择*/
                            gysxzEnumId =FormCap4Kit.getEnumByNameList(AppContext.getSystemProperty("brwh.wbs_enum_gysxz")).get(0).getId().toString();
                            /*合同签署*/
                            htsqEnumId =FormCap4Kit.getEnumByNameList(AppContext.getSystemProperty("brwh.wbs_enum_htqs")).get(0).getId().toString();
                            /*采购订单下发*/
                            cgddxfEnumId =FormCap4Kit.getEnumByNameList(AppContext.getSystemProperty("brwh.wbs_enum_cgddxf")).get(0).getId().toString();
                            /*排产上线*/
                            pcsxEnumId =FormCap4Kit.getEnumByNameList(AppContext.getSystemProperty("brwh.wbs_enum_pcsx")).get(0).getId().toString();
                            /*工厂发货*/
                            gcfhEnumId =FormCap4Kit.getEnumByNameList(AppContext.getSystemProperty("brwh.wbs_enum_gcfh")).get(0).getId().toString();
                            /*客户验收*/
                            khysEnumId =FormCap4Kit.getEnumByNameList(AppContext.getSystemProperty("brwh.wbs_enum_khys")).get(0).getId().toString();

                        }

                        /*采购需求*/
                        if (!"".equals(milestoneStr)) {
                            /*拼接查询sql*/
                            String cgxqsql = "select " + responsiblePersonStr + " as responsiblePersonStr, "
                                    + planEndTimeStr + " as planEndTimeStr, " + actualEndTimeStr + " as actualEndTimeStr from "
                                    + detailTableNameStr + " where " + milestoneStr + " = " + cgxqEnumId +" and formmain_id = "+ masterId;
                            List data = capDataListSqlHelpManager.selectDataList(cgxqsql, null);
                            log.info("采购需求数据查询datasize： " + data.size());
                            if (data.size() > 0) {
                                Map<String, Object> cgxqMap = (Map<String, Object>) data.get(0);
                                String PlanDateValue = StrKit.str(cgxqMap.get("planendtimestr"));
                                String TrueDateValue = StrKit.str(cgxqMap.get("actualendtimestr"));
                                String responsiblePersonStrId = StrKit.str(cgxqMap.get("responsiblepersonstr"));
                                /*处理责任人*/
                                String responsiblePersonName = "";
                                if (!"".equals(responsiblePersonStrId)) {
                                    V3xOrgMember member = orgManager.getMemberById(Long.valueOf(responsiblePersonStrId));
                                    if (member != null) {
                                        responsiblePersonName = member.getName();
                                    }
                                }
                                resultDataMap.put("cgxqPlanEndTime", PlanDateValue);
                                resultDataMap.put("cgxqactualEndTime", TrueDateValue);
                                resultDataMap.put("cgxqResponsiblePerson", responsiblePersonName);
                            }
                        }
                        /*供应商选择*/
                        if (!"".equals(milestoneStr)) {
                            String gysxzsql = "select " + responsiblePersonStr + " as responsiblePersonStr, "
                                    + planEndTimeStr + " as planEndTimeStr, " + actualEndTimeStr + " as actualEndTimeStr from "
                                    + detailTableNameStr + " where " + milestoneStr + " = " + gysxzEnumId+" and formmain_id = "+ masterId;
                            List data = capDataListSqlHelpManager.selectDataList(gysxzsql, null);
                            if (data.size() > 0) {
                                Map<String, Object> gysxzMap = (Map<String, Object>) data.get(0);
                                String PlanDateValue = StrKit.str(gysxzMap.get("planendtimestr"));
                                String TrueDateValue = StrKit.str(gysxzMap.get("actualendtimestr"));
                                String responsiblePersonStrId = StrKit.str(gysxzMap.get("responsiblepersonstr"));
                                /*处理责任人*/
                                String responsiblePersonName = "";
                                if (!"".equals(responsiblePersonStrId)) {
                                    V3xOrgMember member = orgManager.getMemberById(Long.valueOf(responsiblePersonStrId));
                                    if (member != null) {
                                        responsiblePersonName = member.getName();
                                    }
                                }
                                resultDataMap.put("gysPlanEndTime", PlanDateValue);
                                resultDataMap.put("gysactualEndTime", TrueDateValue);
                                resultDataMap.put("gysResponsiblePerson", responsiblePersonName);
                            }
                        }
                        /*合同签署*/
                        if (!"".equals(milestoneStr)) {
                            String htsqsql = "select " + responsiblePersonStr + " as responsiblePersonStr, "
                                    + planEndTimeStr + " as planEndTimeStr, " + actualEndTimeStr + " as actualEndTimeStr from "
                                    + detailTableNameStr + " where " + milestoneStr + " = " + htsqEnumId+" and formmain_id = "+ masterId;
                            List data = capDataListSqlHelpManager.selectDataList(htsqsql, null);
                            if (data.size() > 0) {
                                Map<String, Object> htsqMap = (Map<String, Object>) data.get(0);
                                String PlanDateValue = StrKit.str(htsqMap.get("planendtimestr"));
                                String TrueDateValue = StrKit.str(htsqMap.get("actualendtimestr"));
                                String responsiblePersonStrId = StrKit.str(htsqMap.get("responsiblepersonstr"));
                                /*处理责任人*/
                                String responsiblePersonName = "";
                                if (!"".equals(responsiblePersonStrId)) {
                                    V3xOrgMember member = orgManager.getMemberById(Long.valueOf(responsiblePersonStrId));
                                    if (member != null) {
                                        responsiblePersonName = member.getName();
                                    }
                                }
                                resultDataMap.put("htsqPlanEndTime", PlanDateValue);
                                resultDataMap.put("htsqactualEndTime", TrueDateValue);
                                resultDataMap.put("htsqResponsiblePerson", responsiblePersonName);
                            }
                        }
                        /*采购订单下发*/
                        if (!"".equals(milestoneStr)) {
                            String cgdxxfsql = "select " + responsiblePersonStr + " as responsiblePersonStr, "
                                    + planEndTimeStr + " as planEndTimeStr, " + actualEndTimeStr + " as actualEndTimeStr from "
                                    + detailTableNameStr + " where " + milestoneStr + " = " + cgddxfEnumId+" and formmain_id = "+ masterId;
                            List data = capDataListSqlHelpManager.selectDataList(cgdxxfsql, null);
                            if (data.size() > 0) {
                                Map<String, Object> cgdxxfMap = (Map<String, Object>) data.get(0);
                                String PlanDateValue = StrKit.str(cgdxxfMap.get("planendtimestr"));
                                String TrueDateValue = StrKit.str(cgdxxfMap.get("actualendtimestr"));
                                String responsiblePersonStrId = StrKit.str(cgdxxfMap.get("responsiblepersonstr"));
                                /*处理责任人*/
                                String responsiblePersonName = "";
                                if (!"".equals(responsiblePersonStrId)) {
                                    V3xOrgMember member = orgManager.getMemberById(Long.valueOf(responsiblePersonStrId));
                                    if (member != null) {
                                        responsiblePersonName = member.getName();
                                    }
                                }
                                resultDataMap.put("cgdxxfPlanEndTime", PlanDateValue);
                                resultDataMap.put("cgdxxfactualEndTime", TrueDateValue);
                                resultDataMap.put("cgdxxfResponsiblePerson", responsiblePersonName);
                            }


                        }
                        /*排产上线*/
                        if (!"".equals(milestoneStr)) {
                            String pcsxsql = "select " + responsiblePersonStr + " as responsiblePersonStr, "
                                    + planEndTimeStr + " as planEndTimeStr, " + actualEndTimeStr + " as actualEndTimeStr from "
                                    + detailTableNameStr + " where " + milestoneStr + " = " + pcsxEnumId+" and formmain_id = "+ masterId;
                            List data = capDataListSqlHelpManager.selectDataList(pcsxsql, null);
                            if (data.size() > 0) {
                                Map<String, Object> pcsxMap = (Map<String, Object>) data.get(0);
                                String PlanDateValue = StrKit.str(pcsxMap.get("planendtimestr"));
                                String TrueDateValue = StrKit.str(pcsxMap.get("actualendtimestr"));
                                String responsiblePersonStrId = StrKit.str(pcsxMap.get("responsiblepersonstr"));
                                /*处理责任人*/
                                String responsiblePersonName = "";
                                if (!"".equals(responsiblePersonStrId)) {
                                    V3xOrgMember member = orgManager.getMemberById(Long.valueOf(responsiblePersonStrId));
                                    if (member != null) {
                                        responsiblePersonName = member.getName();
                                    }
                                }
                                resultDataMap.put("pcsxPlanEndTime", PlanDateValue);
                                resultDataMap.put("pcsxactualEndTime", TrueDateValue);
                                resultDataMap.put("pcsxResponsiblePerson", responsiblePersonName);
                            }
                        }
                        /*工厂发货*/
                        if (!"".equals(milestoneStr)) {
                            String gcfhsql = "select " + responsiblePersonStr + " as responsiblePersonStr, "
                                    + planEndTimeStr + " as planEndTimeStr, " + actualEndTimeStr + " as actualEndTimeStr from "
                                    + detailTableNameStr + " where " + milestoneStr + " = " + gcfhEnumId+" and formmain_id = "+ masterId;
                            List data = capDataListSqlHelpManager.selectDataList(gcfhsql, null);
                            if (data.size() > 0) {
                                Map<String, Object> gcfhMap = (Map<String, Object>) data.get(0);
                                String PlanDateValue = StrKit.str(gcfhMap.get("planendtimestr"));
                                String TrueDateValue = StrKit.str(gcfhMap.get("actualendtimestr"));
                                String responsiblePersonStrId = StrKit.str(gcfhMap.get("responsiblepersonstr"));
                                /*处理责任人*/
                                String responsiblePersonName = "";
                                if (!"".equals(responsiblePersonStrId)) {
                                    V3xOrgMember member = orgManager.getMemberById(Long.valueOf(responsiblePersonStrId));
                                    if (member != null) {
                                        responsiblePersonName = member.getName();
                                    }
                                }
                                resultDataMap.put("gcfhPlanEndTime", PlanDateValue);
                                resultDataMap.put("gcfhactualEndTime", TrueDateValue);
                                resultDataMap.put("gcfhResponsiblePerson", responsiblePersonName);
                            }
                        }
                        /*客户验收*/
                        if (!"".equals(milestoneStr)) {
                            String khyxsql = "select " + responsiblePersonStr + " as responsiblePersonStr, "
                                    + planEndTimeStr + " as planEndTimeStr, " + actualEndTimeStr + " as actualEndTimeStr from "
                                        + detailTableNameStr + " where " + milestoneStr + " = " + khysEnumId+" and formmain_id = "+ masterId;
                            List data = capDataListSqlHelpManager.selectDataList(khyxsql, null);
                            if (data.size() > 0) {
                                Map<String, Object> khyxMap = (Map<String, Object>) data.get(0);
                                String PlanDateValue = StrKit.str(khyxMap.get("planendtimestr"));
                                String TrueDateValue = StrKit.str(khyxMap.get("actualendtimestr"));
                                String responsiblePersonStrId = StrKit.str(khyxMap.get("responsiblepersonstr"));
                                /*处理责任人*/
                                String responsiblePersonName = "";
                                if (!"".equals(responsiblePersonStrId)) {
                                    V3xOrgMember member = orgManager.getMemberById(Long.valueOf(responsiblePersonStrId));
                                    if (member != null) {
                                        responsiblePersonName = member.getName();
                                    }
                                }
                                resultDataMap.put("khyxPlanEndTime", PlanDateValue);
                                resultDataMap.put("khyxactualEndTime", TrueDateValue);
                                resultDataMap.put("khyxResponsiblePerson", responsiblePersonName);
                            }
                        }

                        resultDataList.add(resultDataMap);
                    }
                }
            }
        } catch (Exception e) {
        }
        return resultDataList;
    }


}
